﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jiaju.Models.DBModel;
using System.Data.SqlClient;
using jiaju.Models;

namespace jiaju.Models.DAL
{
    public class NewsTypeService
    {
        //获取所有的分类
        public static List<NewsType> getAllType()
        {

            String sql = "select Id,Name from dbo.newsType";
            List<NewsType> list = new List<NewsType>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string name = (string)sdr["Name"];
                NewsType type = new NewsType() { Id = id, Name = name };
                list.Add(type);
            }
            return list;
        }
        public static List<News> getMore()
        {
            string sql = "select Id,Title,Cont,Time from dbo.news ";
            List<News> list = new List<DBModel.News>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Title = (string)sdr["Title"];
                string Cont = (string)sdr["Cont"];
                DateTime Time = (DateTime)sdr["Time"];
                string NianYue = Time.Year + "/" + Time.Month;
                int Day = Time.Day;
                News more = new News()
                {
                    Id = Id,
                    Title = Title,
                    Cont = Cont,
                    NianYue = NianYue,
                    Day = Day
                };
                list.Add(more);
            }
            return list;
        }
        //分页获取数据
        public static List<News> getPageList(int page, int limit)
        {
            string sql = string.Format(@"select top({1}) Id,Title,Cont,Time
                               from dbo.news where Id not in (
                              select top(({0}-1)*{1}) Id from dbo.news
                              )", page, limit);
            List<News> list = new List<DBModel.News>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Title = (string)sdr["Title"];
                string Cont = (string)sdr["Cont"];
                DateTime Time = (DateTime)sdr["Time"];
                string NianYue = Time.Year + "/" + Time.Month;
                int Day = Time.Day;
                News more = new News()
                {
                    Id = Id,
                    Title = Title,
                    Cont = Cont,
                    NianYue = NianYue,
                    Day = Day
                };
                list.Add(more);
            }
            return list;
        }
    
    }
}